package com.com.hemoWork.work1;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class TCPServer {
    public static void main(String[] args) throws Exception {
// 1. 调用ServerSocket(int port)创建一个服务器端套接字，并绑定到指定端口上。
        ServerSocket ss = new ServerSocket(8088);
// 2. 调用accept()，监听连接请求，如果客户端请求连接，则接受连接，返回通信套接字。
        while (true) {
            Socket socket = ss.accept();
            //接收客户端
            InputStream is = socket.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(is);

            char[] buffer = new char[1024];
            int len = 0;
            if ((len = inputStreamReader.read(buffer)) > 0) {
                String str = new String(buffer, 0, len);
                System.out.println("服务端接收数据：" + str);
            }

            OutputStream os = socket.getOutputStream();
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输出要发送给客户端的数据:");
            String msg1 = scanner.next();
            os.write(msg1.getBytes());
            inputStreamReader.close();
            os.close();

        }
    }
}
